﻿// <copyright file="Shape.cs" company="ThorIndustry">Copyright (c) ThorIndustry. All rights reserved.</copyright>
public partial class PuzzleChoiceMenu
{
    private UnityEngine.Vector2 scrollPosition = new UnityEngine.Vector2();

    private bool isDragging = false;
    private bool isDown = false;
    private UnityEngine.Vector3 mousePositionOnDown;
    private float scrollViewportWidth = 0;
    
    private void UpdateScrollOffset(UnityEngine.Vector3 movedOffset)
    {
        this.scrollPosition.x += movedOffset.x * 10 * GuiHelper.GetScreenSizeRatio();

        // We have to limit the scroll to avoid the position to be mainingless.
        if ((this.scrollPosition.x + this.scrollViewportWidth) <= UnityEngine.Screen.width)
        {
            this.scrollPosition.x = -(this.scrollViewportWidth - UnityEngine.Screen.width);
        }

        if (this.scrollPosition.x > 0)
        {
            this.scrollPosition.x = 0;
        }
    }

    private void UpdateScrollView()
    {
        // Drag the puzzles buttons.
        // Mouse first...
        if (!this.isDown)
        {
            // hum hum.
        }

        UnityEngine.Vector3 worldMousePosition = this.GetPosition();

        // Is the mouse button down?
        if (this.IsButtonDown())
        {
            this.mousePositionOnDown = worldMousePosition;
            this.isDown = true;
        }

        if (this.IsButtonUp())
        {
            this.isDown = false;
            this.isDragging = false;
        }

        if (this.isDown)
        {
            if (!this.isDragging)
            {
                // Check if the drag has started.
                if (UnityEngine.Vector3.SqrMagnitude(this.mousePositionOnDown - worldMousePosition) > 0.05f)
                {
                    // Drag started.
                    this.isDragging = true;
                }
            }
            else
            {
                // We are currently moving the selected shape.
                UnityEngine.Vector3 moveOffset = worldMousePosition - this.mousePositionOnDown;
                this.UpdateScrollOffset(moveOffset);

                this.mousePositionOnDown = worldMousePosition;
            }
        }
    }

    private bool IsButtonDown()
    {
#if (UNITY_IPHONE || UNITY_ANDROID)
        return UnityEngine.Input.touchCount == 1 &&
               UnityEngine.Input.touches[0].phase == UnityEngine.TouchPhase.Began;
#else
        return UnityEngine.Input.GetMouseButtonDown(0);
#endif
    }

    private bool IsButtonUp()
    {
#if (UNITY_IPHONE || UNITY_ANDROID)
        return UnityEngine.Input.touchCount == 1 &&
               (UnityEngine.Input.touches[0].phase == UnityEngine.TouchPhase.Ended ||
                UnityEngine.Input.touches[0].phase == UnityEngine.TouchPhase.Canceled);
#else
        return UnityEngine.Input.GetMouseButtonUp(0);
#endif
    }

    private UnityEngine.Vector3 GetPosition()
    {
#if (UNITY_IPHONE || UNITY_ANDROID)
        if (UnityEngine.Input.touchCount == 1)
        {
            return UnityEngine.Camera.main.ScreenToWorldPoint(UnityEngine.Input.touches[0].position);
        }
#endif
        return UnityEngine.Camera.main.ScreenToWorldPoint(UnityEngine.Input.mousePosition);

    }
}
